﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EDiamondModel;
using System.IO;

public partial class Admin_ThucDonSua : System.Web.UI.Page
{
    EDiamondEntities1 db = new EDiamondEntities1();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void dvwThucDon_ItemCreated(object sender, EventArgs e)
    {
        if (dvwThucDon.Rows.Count == 0) return;
        if (dvwThucDon.CurrentMode == DetailsViewMode.ReadOnly)
        {
            ImageButton nutSua = (ImageButton)dvwThucDon.Rows[10].Cells[0].Controls[0];
            ImageButton nutHuy = (ImageButton)dvwThucDon.Rows[10].Cells[0].Controls[2];
            nutHuy.OnClientClick = "if(!confirm('Bạn có thật muốn hủy không?')) return false;";
            nutHuy.ToolTip = "Hủy thông tin";
            nutSua.ToolTip = "Hiệu chỉnh thông tin";
        }
        else if (dvwThucDon.CurrentMode == DetailsViewMode.Edit)
        {
            ImageButton nutGhi = (ImageButton)dvwThucDon.Rows[10].Cells[0].Controls[0];
            ImageButton nutBoQua = (ImageButton)dvwThucDon.Rows[10].Cells[0].Controls[2];
            //nutHuy.OnClientClick = "if(!confirm('Bạn có thật muốn hủy không?')) return false;";
            nutGhi.ToolTip = "Ghi thông tin";
            nutBoQua.ToolTip = "Bỏ qua";
        }
    }
    string p = "";
    protected void dvwThucDon_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        string newName = e.NewValues["TenSanPham"].ToString().Trim();
        string oldName = e.OldValues["TenSanPham"].ToString().Trim();
        if (newName != oldName)
        {
            int d = db.SanPhams.Count(p => p.TenSanPham == newName);
            if (d > 0)
            {
                e.Cancel = true;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Tên sản phẩm này đã có rồi!')", true);
            }
        }
    }
    protected void dvwThucDon_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        int id = (int)e.Keys["SanPhamID"];
        if (db.DatHangCTs.Count(p => p.SanPhamID == id) > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Không hủy được vì đã có thông tin đặt hàng liên quan!')", true);
            e.Cancel = true;
        }
    }
    protected void EntityDataSourceDSThucDon_Updating(object sender, EntityDataSourceChangingEventArgs e)
    {
        SanPham sp = (SanPham)e.Entity;
        sp.BiDanh = Thuvien.BoDauTiengViet(sp.TenSanPham);
        FileUpload f = (FileUpload)dvwThucDon.Rows[6].Cells[1].FindControl("FileUpload1");
        if (f.HasFile)
        {
            sp.Hinh = sp.BiDanh + Path.GetExtension(f.FileName);
            string p = Server.MapPath("~/Photos/SanPham/") + sp.Hinh;
        }
    }
    protected void EntityDataSourceDSThucDon_Updated(object sender, EntityDataSourceChangedEventArgs e)
    {
        if (p.Length > 0)
        {
            FileUpload f = (FileUpload)dvwThucDon.Rows[6].Cells[1].FindControl("FileUpload");
            f.SaveAs(p);
        }
    }
    protected void DSThucDon_Updated(object sender, EntityDataSourceChangedEventArgs e)
    {
        if (p.Length > 0)
        {
            FileUpload f = (FileUpload)dvwThucDon.Rows[6].Cells[1].FindControl("FileUpload");
            f.SaveAs(p);
        }
    }
    protected void DSThucDon_Updating(object sender, EntityDataSourceChangingEventArgs e)
    {
        SanPham sp = (SanPham)e.Entity;
        sp.BiDanh = Thuvien.BoDauTiengViet(sp.TenSanPham);
        FileUpload f = (FileUpload)dvwThucDon.Rows[6].Cells[1].FindControl("FileUpload1");
        if (f.HasFile)
        {
            sp.Hinh = sp.BiDanh + Path.GetExtension(f.FileName);
            string p = Server.MapPath("~/Photos/SanPham/") + sp.Hinh;
        }
    }
}